package com.siloam.controller;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.siloam.module.Article;
import com.siloam.module.ArticleCategory;
import com.siloam.module.User;
import com.siloam.service.ArticleCategoryService;
import com.siloam.service.ArticleService;
import com.siloam.vo.ArticleVO;
import com.siloam.vo.PageParam;
import com.siloam.vo.PageVO;

@Controller
@RequestMapping("/web/article/article.do")
public class ArticleController extends BaseController{

	@Resource
	private ArticleService articleService;
	
	@Resource
	private ArticleCategoryService articleCategoryService;
	
	public ArticleService getArticleService() {
		return articleService;
	}

	public void setArticleService(ArticleService articleService) {
		this.articleService = articleService;
	}

	public ArticleCategoryService getArticleCategoryService() {
		return articleCategoryService;
	}

	public void setArticleCategoryService(
			ArticleCategoryService articleCategoryService) {
		this.articleCategoryService = articleCategoryService;
	}

	@RequestMapping(params="method=addNotice")
	@ResponseBody
	public String addNotice(Article article){
		//TODO 
		User u = new User();
		u.setId(6);
		article.setAuthor(u);
		article.setDate(new Date());
		article.setArticleType(Article.TYPE_SILOAM_NOTIC);
		articleService.add(article);
		return SUCCESS;
	}
	
	@RequestMapping(params="method=listArticleCategorys")
	@ResponseBody
	public List<ArticleCategory> listArticleCategorys(){
		return articleCategoryService.findAll();
	}
	
	@RequestMapping(params="method=writeArticle")
	@ResponseBody
	public String writeArticle(Article article){
		//TODO 
		User u = new User();
		u.setId(6);
		article.setAuthor(u);
		article.setDate(new Date());
		article.setArticleType(Article.TYPE_PERSON_ARTICLE);
		articleService.add(article);
		return SUCCESS;
	}
	
	@RequestMapping(params="method=findUserArticles")
	@ResponseBody
	public PageVO<ArticleVO> findUserArticles(PageParam param){
		int userId = 6;//TODO
		return articleService.findUserArticles(userId,param.getRowBeginIndex(), param.getRowCount());
	}
	
	@RequestMapping(params="method=loadArticle")
	public String loadArticle(int id, ModelMap map){
		map.put("article", articleService.load(id));
		return "/web/article/articleDetail";
	}
	
	@RequestMapping(params="method=deleteArticle")
	@ResponseBody
	public String deleteArticle(int id){
		articleService.delete(id);
		return SUCCESS;
	}
	
	@RequestMapping(params="method=initEditArticle")
	public String initEditArticle(int id, ModelMap map){
		map.put("article", articleService.load(id));
		return "/web/article/editArticle";
	}
	
	@RequestMapping(params="method=updateArticle")
	@ResponseBody
	public String updateArticle(Article article){
		Article old = articleService.loadArticle(article.getId());
		old.setTitle(article.getTitle());
		old.setContent(article.getContent());
		old.setArticleCategory(article.getArticleCategory());
		articleService.update(old);
		return SUCCESS;
	}
	
}
